package com.kerrykidz.system.util.printReceipt;

import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.PrinterJob;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class PrinterAction {
	// private PrinterMessage printerMessage;
	// private List<PrinterMessage> list;
	public static void main(String[] args) {
		List<PrinterMessage> list = new ArrayList<PrinterMessage>();
		PrinterMessage bean1 = new PrinterMessage();
		bean1.setOrderNo("110");
		bean1.setAdmin("余书慧");
		bean1.setDate(PrinterDao.formatDateTime(new Date()));
		bean1.setCashier("张三丰");
		bean1.setBrand("Kerry Kidz");
		bean1.setTotalPrice(60.00);
		bean1.setShop("天津店");
		bean1.setTableName("A2");
		bean1.setTeaPrice(2.00);
		bean1.setCash("0.00");
		bean1.setCup("建设银行");
		bean1.setPaidIn(50.00);
		bean1.setItemName("蛋黄蝶");
		System.out.println("========bean1:" + bean1);
		list.add(0, bean1);
		PrinterMessage bean2 = new PrinterMessage();
		bean2.setOrderNo("110");
		bean2.setAdmin("余书慧");
		bean2.setBrand("寿师傅时尚日本料理");
		bean2.setTotalPrice(60.00);
		bean2.setShop("蓝色港湾店");
		bean2.setTableName("A2");
		bean2.setTeaPrice(2.00);
		bean2.setCash("0.00");
		bean2.setCup("建设银行");
		bean2.setPaidIn(50.00);
		bean2.setItemName("黄蝶");
		list.add(1, bean2);
		 
		System.out.println("========list:" + list);
		print(list);
	}

	/**
	 * 用于将商品零售进行进行打印
	 * 
	 */
	public static void print(List<PrinterMessage> list) {
		// 通俗理解就是书、文档
		Book book = new Book();
		// 设置成竖打
		PageFormat pf = new PageFormat();
		pf.setOrientation(PageFormat.PORTRAIT); // LANDSCAPE表示竖打;PORTRAIT表示横打;REVERSE_LANDSCAPE表示打印空白
		// 通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。
		Paper p = new Paper();
		int length = printSize(list);
		System.out.println("=========length:" + length);
		p.setSize(200, length); // 纸张大小(590, 840)表示A4纸
		p.setImageableArea(10, 10, 227, length); // A4(595 X
		// 842)设置打印区域，其实0，0应该是72，72
		// ，因为A4纸的默认X,Y边距是72
		pf.setPaper(p);
		// // 把 PageFormat 和 Printable 添加到书中，组成一个页面
		book.append(new PrinterDao(list), pf);
		// // 获取打印服务对象
		PrinterJob job = PrinterJob.getPrinterJob();
		// // 设置打印类
		job.setPageable(book);
		try {
			// // 可以用printDialog显示打印对话框，在用户确认后打印；也可以直接打印
			boolean a = job.printDialog();
			System.out.println(job.getPrintService());
			// job.setPrintService(new PrintService("\\10.20.12.12\ISSSH HP
			// LaserJet 1522NF"));
			// job.setJobName(jobName)
			if (a) {
				job.print();
			}
			// PrintServiceLookup
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static Integer printSize(List<PrinterMessage> list) {
		int k = 20;
		if (list.size() != 0) {
			k = k * list.size();
		}
		return 160 + k;
	}
}
